
clear
clc

% Import Data 
%==========================================================================

Y_market = readtable('results_c0_c1.csv');

% Format and sort income
%--------------------------------------------------------------------------

Y = Y_market(:, 5:104);
Y = table2array(Y);
Y_sorted = sort(Y, 2);

% Import c0 and c1
%--------------------------------------------------------------------------

c0 = Y_market(:, 106);
c0 = table2array(c0);
c1 = Y_market(:, 107);
c1 = table2array(c1);


% Get Shares
%==========================================================================

s_20 = c0 + c1 .* Y_sorted(:, 20);
s_80 = c0 + c1 .* Y_sorted(:, 80);
s = c0 + c1 .* Y_sorted;
s_true = Y_market(:, 105);
s_true = table2array(s_true);
s_20(isnan(s_20)) = 0;
s_80(isnan(s_80)) = 0;
s(isnan(s)) = 0;
c0(isnan(c0)) = 0;
c1(isnan(c1)) = 0;


% Format and save
%==========================================================================

share_summary = [table2array(Y_market(:, 1:4)), s_true, c0, c1, s];
data_save = array2table(share_summary);
data_save.Properties.VariableNames(1:7) = {'pc8', 'year', 'quarter', 'declarant', 's_true', 'c0', 'c1'};       
writetable(data_save, 'share_summary_gini.csv', 'Delimiter',',','QuoteStrings',true);



